<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>[go] golang踩坑记录 - Never Give Up</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="ZhangKQ" /><meta name="description" content="golang踩坑记录 一.json问题 1.json中存在单引号,json.Unmarshal解析至结构体报错 正常的解析过程 1 2 3 4 5 6 7 8 9 10" /><meta name="keywords" content="开发语言, 后端, golang, go语言, 踩坑" />






<meta name="generator" content="Hugo 0.92.0 with theme even" />


<link rel="canonical" href="https://blog.nevergiveup.tech/post/dev_language/golang/golang%E7%9A%84json%E8%A7%A3%E6%9E%90%E8%87%B3%E7%BB%93%E6%9E%84%E4%BD%93%E6%8A%A5%E9%94%99%E8%A7%A3%E5%86%B3/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="[go] golang踩坑记录" />
<meta property="og:description" content="golang踩坑记录 一.json问题 1.json中存在单引号,json.Unmarshal解析至结构体报错 正常的解析过程 1 2 3 4 5 6 7 8 9 10" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://blog.nevergiveup.tech/post/dev_language/golang/golang%E7%9A%84json%E8%A7%A3%E6%9E%90%E8%87%B3%E7%BB%93%E6%9E%84%E4%BD%93%E6%8A%A5%E9%94%99%E8%A7%A3%E5%86%B3/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2020-10-14T15:37:56+08:00" />
<meta property="article:modified_time" content="2022-02-10T16:37:56+08:00" />

<meta itemprop="name" content="[go] golang踩坑记录">
<meta itemprop="description" content="golang踩坑记录 一.json问题 1.json中存在单引号,json.Unmarshal解析至结构体报错 正常的解析过程 1 2 3 4 5 6 7 8 9 10"><meta itemprop="datePublished" content="2020-10-14T15:37:56+08:00" />
<meta itemprop="dateModified" content="2022-02-10T16:37:56+08:00" />
<meta itemprop="wordCount" content="275">
<meta itemprop="keywords" content="开发语言,后端,golang,go语言,踩坑," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="[go] golang踩坑记录"/>
<meta name="twitter:description" content="golang踩坑记录 一.json问题 1.json中存在单引号,json.Unmarshal解析至结构体报错 正常的解析过程 1 2 3 4 5 6 7 8 9 10"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Never Give Up</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/remark/">
        <li class="mobile-menu-item">随言碎语</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Never Give Up</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/remark/">随言碎语</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">[go] golang踩坑记录</h1>

      <div class="post-meta">
        <span class="post-time"> 2020-10-14 </span>
        <div class="post-category">
            <a href="/categories/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/"> 开发语言 </a>
            <a href="/categories/golang/"> golang </a>
            </div>
          <span class="more-meta"> 约 275 字 </span>
          <span class="more-meta"> 预计阅读 1 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#golang踩坑记录">golang踩坑记录</a>
      <ul>
        <li><a href="#一json问题">一.json问题</a>
          <ul>
            <li><a href="#1json中存在单引号jsonunmarshal解析至结构体报错">1.json中存在单引号,json.Unmarshal解析至结构体报错</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h1 id="golang踩坑记录">golang踩坑记录</h1>
<h2 id="一json问题">一.json问题</h2>
<h3 id="1json中存在单引号jsonunmarshal解析至结构体报错">1.json中存在单引号,json.Unmarshal解析至结构体报错</h3>
<p>正常的解析过程</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="kd">type</span> <span class="nx">ClusterGeneralInfo</span> <span class="kd">struct</span> <span class="p">{</span>
	<span class="nx">InitStatus</span> <span class="kt">string</span>  <span class="s">`json:&#34;init_status&#34;`</span>
	<span class="nx">InitTime</span>   <span class="kt">float64</span> <span class="s">`json:&#34;init-time&#34;`</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">TestJsonUnmarshal</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span>  <span class="p">{</span>
	<span class="nx">ClusterGeneralInfo</span> <span class="o">:=</span> <span class="nx">ClusterGeneralInfo</span><span class="p">{}</span>
	<span class="nx">value</span> <span class="o">:=</span> <span class="s">&#34;{&#39;init_status&#39;: &#39;enable&#39;, &#39;init-time&#39;: 1638529388.948411}&#34;</span>
	<span class="c1">//value = `{&#34;init_status&#34;: &#34;enable&#34;, &#34;init-time&#34;: 1638529388.948411}`
</span><span class="c1"></span>	<span class="nx">err</span> <span class="o">:=</span> <span class="nx">json</span><span class="p">.</span><span class="nf">Unmarshal</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">value</span><span class="p">),</span> <span class="o">&amp;</span><span class="nx">ClusterGeneralInfo</span><span class="p">)</span>
	<span class="nx">spew</span><span class="p">.</span><span class="nf">Dump</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span>
	<span class="nx">spew</span><span class="p">.</span><span class="nf">Dump</span><span class="p">(</span><span class="nx">ClusterGeneralInfo</span><span class="p">)</span>
	<span class="nx">require</span><span class="p">.</span><span class="nf">NoError</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>报错</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="o">===</span> RUN   TestJsonUnmarshal
<span class="o">(</span>string<span class="o">)</span> <span class="o">(</span><span class="nv">len</span><span class="o">=</span>57<span class="o">)</span> <span class="s2">&#34;{&#39;init_status&#39;: &#39;enable&#39;, &#39;init-time&#39;: 1638529388.948411}&#34;</span>
<span class="o">(</span>test.ClusterGeneralInfo<span class="o">)</span> <span class="o">{</span>
 InitStatus: <span class="o">(</span>string<span class="o">)</span> <span class="s2">&#34;&#34;</span>,
 InitTime: <span class="o">(</span>float64<span class="o">)</span> <span class="m">0</span>
<span class="o">}</span>
    node_test.go:26: 
        	Error Trace:	node_test.go:26
        	Error:      	Received unexpected error:
        	            	invalid character <span class="s1">&#39;\&#39;</span><span class="err">&#39;</span> looking <span class="k">for</span> beginning of object key string
        	Test:       	TestJsonUnmarshal
--- FAIL: TestJsonUnmarshal <span class="o">(</span>0.00s<span class="o">)</span>

FAIL
</code></pre></td></tr></table>
</div>
</div><p>json.Unmarshal方法只能解析双引号包裹的标准json,所以做以下处理</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">TestJsonUnmarshal</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span>  <span class="p">{</span>
	<span class="nx">ClusterGeneralInfo</span> <span class="o">:=</span> <span class="nx">ClusterGeneralInfo</span><span class="p">{}</span>
	<span class="nx">value</span> <span class="o">:=</span> <span class="s">&#34;{&#39;init_status&#39;: &#39;enable&#39;, &#39;init-time&#39;: 1638529388.948411}&#34;</span>
	<span class="c1">//value = `{&#34;init_status&#34;: &#34;enable&#34;, &#34;init-time&#34;: 1638529388.948411}`
</span><span class="c1"></span>    <span class="c1">//做替换处理,将单引号替换为双引号
</span><span class="c1"></span>	<span class="nx">value</span> <span class="p">=</span> <span class="nx">strings</span><span class="p">.</span><span class="nf">Replace</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="s">&#34;&#39;&#34;</span><span class="p">,</span> <span class="s">&#34;\&#34;&#34;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
	<span class="nx">err</span> <span class="o">:=</span> <span class="nx">json</span><span class="p">.</span><span class="nf">Unmarshal</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">value</span><span class="p">),</span> <span class="o">&amp;</span><span class="nx">ClusterGeneralInfo</span><span class="p">)</span>
	<span class="nx">spew</span><span class="p">.</span><span class="nf">Dump</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span>
	<span class="nx">spew</span><span class="p">.</span><span class="nf">Dump</span><span class="p">(</span><span class="nx">ClusterGeneralInfo</span><span class="p">)</span>
	<span class="nx">require</span><span class="p">.</span><span class="nf">NoError</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">ZhangKQ</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-02-10
        
    </span>
  </p>
  
  
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/wechat-qr-code.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/alipay-qr-code.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/">开发语言</a>
          <a href="/tags/%E5%90%8E%E7%AB%AF/">后端</a>
          <a href="/tags/golang/">golang</a>
          <a href="/tags/go%E8%AF%AD%E8%A8%80/">go语言</a>
          <a href="/tags/%E8%B8%A9%E5%9D%91/">踩坑</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/editor/typora%E7%94%BB%E5%9B%BEmermaid/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">[typora] Typora画图-Mermaid流程图</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/version_controller/git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">
            <span class="next-text nav-default">[git]Git常用命令</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wdyxzkq@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/dysoso" class="iconfont icon-github" title="github"></a>
      <a href="https://gitee.com/dysoso" class="iconfont icon-gitlab" title="gitlab"></a>
  <a href="https://blog.nevergiveup.tech/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://blog.nevergiveup.tech/">blog.nevergiveup.tech</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2021 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span><a href="https://beian.miit.gov.cn/">蜀ICP备2021005948号-1</a></span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'never-give-up', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
